package com.wang.users;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wang.users.pojo.*;
import com.wang.users.service.AadminsService;
import com.wang.users.service.AroleService;
import lombok.extern.log4j.Log4j2;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 专门对用户模块做折测试
 * @author Zjx
 */
@SpringBootTest
@Log4j2
public class UsersDbService {
    @Autowired
    private AroleService roleService;
    @Autowired
    private AadminsService adminsService;

    /**
     * 查询所有的角色
     */
    @Test
    public void rolelist(){
        IPage<Arole> page = new Page<>(1,3);
        /* 搜索 */
        QueryWrapper<Arole> wrapper = new QueryWrapper<>();
        /* like */
        wrapper.like("name", "mybatisPlus");
        /* 有分页 */
        List<Arole> dataList = this.roleService.list(page,wrapper);
        log.info("==总条数:{},总页数:{},当前页:{},每页条数:{}==", page.getTotal(), page.getPages(), page.getCurrent(),
                page.getSize());
        for (Arole dataTemp : dataList) {
            log.info("==数据:{}==", dataTemp);
        }
    }

    /**
     * 查询所有的角色
     */
    @Test
    public void findRoleList(){
        IPage<Arole> page = new Page<>(1,3);
        AroleQuery roleQuery = new AroleQuery();
        Arole entity = roleQuery.getEntity();
        /* 状态搜索 */
        entity.setStatus(AroleEnum.STATUS_ENABLE.getCode());
        /* 关键字搜索 */
        roleQuery.setKeyword("%my%");
        /* 时间范围 */
        Calendar calendar = Calendar.getInstance();
        Date pubTimeEd = calendar.getTime();
        calendar.add(Calendar.DATE, -10);
        Date pubTimeSt = calendar.getTime();
        roleQuery.setPubTimeSt(pubTimeSt);
        roleQuery.setPubTimeEd(pubTimeEd);

        roleQuery.setOrderBy("idAsc");

        /* 有分页 */
//        List<Arole> dataList = this.roleService.findList(page,roleQuery);
//        log.info("==总条数:{},总页数:{},当前页:{},每页条数:{}==", page.getTotal(), page.getPages(), page.getCurrent(),
//                page.getSize());
        List<Arole> dataList = this.roleService.findList(roleQuery);
        for (Arole dataTemp : dataList) {
            log.info("==数据:{}==", dataTemp);
        }
    }

    /**
     * 根据Id查询角色
     */
    @Test
    public void getByIdRole(){
        int idInt = 11 ;
        Arole role = this.roleService.getById(idInt);
        log.info("==角色信息:{}==", role);
    }

    /**
     * 保存一条记录
     */
    @Test
    public void saveOneRole(){
        Arole role = new Arole();
        role.setName("mybatisPlus");
        role.setStatus(AroleEnum.STATUS_ENABLE.getCode());
        boolean dbFlag = this.roleService.save(role);
        log.info("=添加=角色:id:{},dbFalg:{}==", role.getId(), dbFlag);
    }

    /**
     * 更新一条记录
     */
    @Test
    public void updateByIdRole(){
        Arole role = this.roleService.getById("1");
        role.setName(role.getName() + "===修改的");
        boolean dbFlag = this.roleService.updateById(role);
        log.info("=更新=角色:id:{},dbFalg:{}==", role.getId(), dbFlag);
    }

    /**
     * 删除一条记录
     */
    @Test
    public void removeByIdRole(){
        Arole role = this.roleService.getById("1");
        role.setName(role.getName() + "===修改的");
        boolean dbFlag = this.roleService.removeById(role);
        log.info("=删除=角色:id:{},dbFalg:{}==", role.getId(), dbFlag);
    }

    /**
     * 查询所有的管理员
     */
    @Test
    public void adminslist(){
        IPage<Aadmins> page = new Page<>(1,3);
        /* 搜索 */
        QueryWrapper<Aadmins> wrapper = new QueryWrapper<>();
        /* like */
//        wrapper.like("name", "mybatisPlus");
        /* 有分页 */
        List<Aadmins> dataList = this.adminsService.list(page,wrapper);
        log.info("==总条数:{},总页数:{},当前页:{},每页条数:{}==", page.getTotal(), page.getPages(), page.getCurrent(),
                page.getSize());
        for (Aadmins dataTemp : dataList) {
            log.info("==数据:{}==", dataTemp);
        }
    }

    /**
     * 查询所有的管理员
     */
    @Test
    public void findAdminsList(){
        IPage<Aadmins> page = new Page<>(1,3);
        AadminsQuery adminsQuery = new AadminsQuery();
        Aadmins entity = adminsQuery.getEntity();
        /* 状态搜索 */
        entity.setStatus(AadminsEnum.STATUS_ENABLE.getCode());
        /* 关键字搜索 */
        adminsQuery.setKeyword("%my%");
        /* 时间范围 */
        Calendar calendar = Calendar.getInstance();
        Date pubTimeEd = calendar.getTime();
        calendar.add(Calendar.DATE, -10);
        Date pubTimeSt = calendar.getTime();
        adminsQuery.setPubTimeSt(pubTimeSt);
        adminsQuery.setPubTimeEd(pubTimeEd);

        adminsQuery.setOrderBy("idAsc");

        /* 有分页 */
//        List<Aadmins> dataList = this.adminsService.findList(page,adminsQuery);
//        log.info("==总条数:{},总页数:{},当前页:{},每页条数:{}==", page.getTotal(), page.getPages(), page.getCurrent(),
//                page.getSize());
        List<Aadmins> dataList = this.adminsService.findList(adminsQuery);
        for (Aadmins dataTemp : dataList) {
            log.info("==数据:{}==", dataTemp);
        }
    }

    /**
     * 根据Id查询管理员
     */
    @Test
    public void getByIdAdmins(){
        int idInt = 4 ;
        Aadmins admins = this.adminsService.getById(idInt);
        log.info("==管理员信息:{}==", admins);
        Arole role = admins.getRole();
        log.info("==角色信息:{}==", role);
    }

    /**
     * 保存一条记录
     */
    @Test
    public void saveOneAdmins(){
        Aadmins admins = new Aadmins();
        admins.setNickName("mybatisPlus");
        admins.setStatus(AadminsEnum.STATUS_ENABLE.getCode());
        boolean dbFlag = this.adminsService.save(admins);
        log.info("=添加=管理员:id:{},dbFalg:{}==", admins.getId(), dbFlag);
    }

    /**
     * 更新一条记录
     */
    @Test
    public void updateByIdAdmins(){
        Aadmins admins = this.adminsService.getById("1");
        admins.setNickName(admins.getNickName() + "===修改的");
        boolean dbFlag = this.adminsService.updateById(admins);
        log.info("=更新=管理员:id:{},dbFalg:{}==", admins.getId(), dbFlag);
    }

    /**
     * 删除一条记录
     */
    @Test
    public void removeByIdAdmins(){
        Aadmins admins = this.adminsService.getById("1");
        boolean dbFlag = this.adminsService.removeById(admins);
        log.info("=删除=管理员:id:{},dbFalg:{}==", admins.getId(), dbFlag);
    }
}
